﻿Public Class frmQuanLyNhom
    Public fMain As frmMain
    Public Sub New(ByVal f As frmMain)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        fMain = f
    End Sub

    Private Sub frmQuanLyNhom_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'lay ten cua cac menu
        chkMenu.Items.Clear()
        For Each dong As DataRow In fMain.dtSet.Tables(s_tblMenu).Rows
            'them vao danh muc
            chkMenu.Items.Add(dong(1).ToString)
        Next

        'lay ten cac nhom va dua vao cboBox
        cbTenNhom.DisplayMember = "TenNhom"
        cbTenNhom.ValueMember = "Nhom_ID"
        cbTenNhom.DataSource = fMain.dtSet.Tables(s_tblNhom)
        If fMain.dtSet.Tables(s_tblNhom).Rows.Count > 0 Then
            cbTenNhom.SelectedIndex = 0
        End If
    End Sub

    Private Sub CapNhatMenuCheckTheoNhom()
        'bo het tat ca cac lua chon truoc
        For i As Integer = 0 To chkMenu.Items.Count - 1
            chkMenu.SetItemChecked(i, False)
        Next

        If (cbTenNhom.SelectedIndex <> -1) Then
            'lay ma nhom cua vi tri da duoc chon
            Dim nhom As Integer = cbTenNhom.SelectedValue
            Dim menus As New ArrayList()
            For Each dong As DataRow In fMain.dtSet.Tables(s_tblNhomMenu).Rows
                If (dong(0) = nhom) Then
                    'lay ra ma cua cac menu ma nhom do duoc phep su dung
                    menus.Add(dong(2))
                End If
            Next
            'set check cho cac menu
            For Each s As Object In menus
                For i As Integer = 0 To chkMenu.Items.Count - 1
                    If chkMenu.Items(i).ToString.Trim() = s.ToString.Trim() Then
                        chkMenu.SetItemChecked(i, True)
                    End If
                Next
            Next
        End If
    End Sub

    Private Sub cbTenNhom_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbTenNhom.SelectedIndexChanged
        If cbTenNhom.SelectedIndex <> -1 Then
            CapNhatMenuCheckTheoNhom()
        Else
            'Xoa het cac check quyen
            For i As Integer = 0 To chkMenu.Items.Count - 1
                chkMenu.SetItemChecked(i, False)
            Next
        End If
    End Sub

    Public Sub LamMoi()
        cbTenNhom.Update()
        CapNhatMenuCheckTheoNhom()
    End Sub

    Private Sub btCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCapNhat.Click
        'xoa het trong tblNhom va cap nhat cai moi
        Dim sUpdate As String = ""
        Dim nhom As Integer = cbTenNhom.SelectedValue
        For Each dong As DataRow In fMain.dtSet.Tables(s_tblNhomMenu).Rows
            If (dong(0) = nhom) Then
                dong.Delete()
            End If
        Next
        fMain.dtSet.AcceptChanges()
        sUpdate = "delete from " + s_tblNhomMenu + " where Nhom_ID=" & nhom

        For Each item As Object In chkMenu.CheckedItems
            'kiem tra xem Trich luu du lieu co duoc check hay khong
            If (item.ToString().Trim = "Trích lưu dữ liệu") Then
                MessageBox.Show("Chức năng trích  lưu dữ liệu chỉ có tác dụng khi bạn cấp quyền Admin cho người sử dụng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
            'lay ra ma menu
            Dim id As Integer = Convert.ToInt16(fMain.dtSet.Tables(s_tblMenu).Rows(chkMenu.Items.IndexOf(item))(0))
            'them mot bo moi vao bang tblNSD_Nhom
            Dim dong As DataRow = fMain.dtSet.Tables(s_tblNhomMenu).NewRow()
            dong("Nhom_ID") = nhom
            dong("Menu_ID") = id
            dong("MenuName") = item.ToString()
            fMain.dtSet.Tables(s_tblNhomMenu).Rows.Add(dong)
            sUpdate &= " insert into " + s_tblNhomMenu + " values(" & nhom & "," & id & ")"
        Next

        fMain.dtSet.AcceptChanges()

        If (sUpdate <> "") Then
            If (XL_DULIEU.ThucHienLenh(sUpdate) = True) Then
                'neu ghi thanh cong thi bao
                MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                fMain.cboBan.Refresh()
                fMain.CapNhatLaiCheckQuyen()
            Else
                MessageBox.Show("Cập nhật không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End If
    End Sub

    Private Sub btXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btXoa.Click
        If (MessageBox.Show("Có thực sự muốn xóa nhóm này?", "Xác nhận xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
            Dim maNhom = cbTenNhom.SelectedValue

            Dim cauLenh As String = ""
            cauLenh &= " delete from " + s_tblNhomMenu + " where Nhom_ID =" & maNhom
            cauLenh &= " update " + s_tblNguoiDung + " set nhom_id = null where nhom_id =" & maNhom
            cauLenh &= " delete from " + s_tblNhom + " where Nhom_ID =" & maNhom

            Try
                'xoa trong bang s_tblNhom
                fMain.dtSet.Tables(s_tblNhom).Rows(cbTenNhom.SelectedIndex).Delete()

                'xoa trong bang s_tblNhomMenu
                For Each dong As DataRow In fMain.dtSet.Tables(s_tblNhomMenu).Rows
                    If (dong(0) = maNhom) Then
                        dong.Delete()
                    End If
                Next

                'xoa trong bang s_tblNhomMenu
                For Each dongUser As DataRow In fMain.dtSet.Tables(s_tblNguoiDung).Rows
                    Try
                        If (dongUser("nhom_id") = maNhom) Then
                            'loai bo nhom_id cua User nay
                            dongUser("nhom_id") = -1
                        End If
                    Catch ex As Exception
                    End Try
                Next

                'cap nhat lai dataset
                fMain.dtSet.AcceptChanges()

                'xoa trong co so du lieu
                If (XL_DULIEU.ThucHienLenh(cauLenh) = True) Then
                    MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK)
                    fMain.cboBan.Refresh()
                    fMain.CapNhatTrangThai(fMain.listNSD.SelectedValue)
                    CapNhatMenuCheckTheoNhom()
                Else
                    MessageBox.Show("Xóa không thành công", "Thông báo", MessageBoxButtons.OK)
                End If
            Catch ex As Exception
                MessageBox.Show("Xóa không thành công", "Thông báo", MessageBoxButtons.OK)
            End Try
        End If
    End Sub

    Private Sub btThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btThoat.Click
        Me.Close()
    End Sub

    Private Sub ChonHetToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChonHetToolStripMenuItem.Click
        For i As Integer = 0 To chkMenu.Items.Count - 1
            chkMenu.SetItemChecked(i, True)
        Next
    End Sub

    Private Sub HuyChonToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HuyChonToolStripMenuItem.Click
        For i As Integer = 0 To chkMenu.Items.Count - 1
            chkMenu.SetItemChecked(i, False)
        Next
    End Sub
End Class